package com.jeesite.modules.mx.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.mx.entity.MxTeacher;
import com.jeesite.modules.mx.dao.MxTeacherDao;

/**
 * teacherService
 * @author ccz
 * @version 2025-03-21
 */
@Service
public class MxTeacherService extends CrudService<MxTeacherDao, MxTeacher> {

	@Autowired
	private MxTeacherDao mxTeacherDao;



	/**
	 * 获取单条数据
	 * @param mxTeacher
	 * @return
	 */
	@Override
	public MxTeacher get(MxTeacher mxTeacher) {
		return super.get(mxTeacher);
	}
	
	/**
	 * 查询分页数据
	 * @param mxTeacher 查询条件
	 * @param mxTeacher.page 分页对象
	 * @return
	 */
	@Override
	public Page<MxTeacher> findPage(MxTeacher mxTeacher) {
		return super.findPage(mxTeacher);
	}
	
	/**
	 * 查询列表数据
	 * @param mxTeacher
	 * @return
	 */
	@Override
	public List<MxTeacher> findList(MxTeacher mxTeacher) {
		return super.findList(mxTeacher);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param mxTeacher
	 */
	@Override
	@Transactional
	public void save(MxTeacher mxTeacher) {
		super.save(mxTeacher);
	}
	
	/**
	 * 更新状态
	 * @param mxTeacher
	 */
	@Override
	@Transactional
	public void updateStatus(MxTeacher mxTeacher) {
		super.updateStatus(mxTeacher);
	}
	
	/**
	 * 删除数据
	 * @param mxTeacher
	 */
	@Override
	@Transactional
	public void delete(MxTeacher mxTeacher) {
		super.delete(mxTeacher);
	}








	private static final int BATCH_SIZE = 1000; // 每批次插入的数据量
	private static final int THREAD_POOL_SIZE = 10; // 线程池大小

	public void wudi(MxTeacher mxTeacher) {
		ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
		List<Future<?>> futures = new ArrayList<>();

		for (int i = 0; i < 50000 / BATCH_SIZE; i++) {
			futures.add(executorService.submit(() -> {
				List<MxTeacher> teachers = generateRandomTeachers(BATCH_SIZE);
				mxTeacherDao.saveAll(teachers);
			}));
		}

		for (Future<?> future : futures) {
			try {
				future.get(); // 等待所有线程完成
			} catch (InterruptedException | ExecutionException e) {
				e.printStackTrace();
			}
		}

		executorService.shutdown(); // 关闭线程池
	}

	private List<MxTeacher> generateRandomTeachers(int count) {
		List<MxTeacher> teachers = new ArrayList<>();
		Random random = new Random();
		UUID uuid = UUID.randomUUID();
		for (int i = 0; i < count; i++) {
			MxTeacher teacher = new MxTeacher();
			teacher.setMxId(uuid.randomUUID().toString()); // 使用 UUID 生成唯一ID
			teacher.setMxName("Teacher" + random.nextInt(10000));
			teacher.setMxHometown("Hometown" + random.nextInt(100));
			teacher.setMxRegion("Region" + random.nextInt(10));
			teacher.setMxScore(String.format("%.2f", random.nextDouble() * 100)); // 生成0-100的分数
			teacher.setMxUnit("Unit" + random.nextInt(100));
			teachers.add(teacher);
		}
		return teachers;
	}


}